package org.flow.basic.thread.virtual;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author xl-9527
 * @since 2024/10/8
 **/
public class VirtualSimpleDemo {

    private final Logger log = LoggerFactory.getLogger(VirtualSimpleDemo.class);

    public Thread createNormalThread(final Runnable runnable) {
        log.info("createVirtualThread method is run and is ok");
        if (Objects.isNull(runnable)) {
            return new Thread(this.defTask());
        }
        return new Thread(runnable);
    }

    public void startVirtualThread(final Thread thread) {
        thread.start();
    }

    public Executor createVirtualExecutor() {
        log.info("createVirtualExecutor method is run and is ok");
        return Executors.newVirtualThreadPerTaskExecutor();
    }

    public Runnable defTask() {
        return () -> log.info("virtual is running");
    }
}
